package com.zhouchao.transfer.factory;


import com.zhouchao.transfer.annotation.Autowired;
import com.zhouchao.transfer.annotation.Component;
import com.zhouchao.transfer.annotation.Transactional;
import com.zhouchao.transfer.utils.TransactionManager;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 应癫
 * <p>
 * <p>
 * 代理对象工厂：生成代理对象的
 */
@Component("proxyFactory")
public class ProxyFactory {

    @Autowired
    private TransactionManager transactionManager;

    public Object getProxy(Object obj) {
        Class<?> clazz = obj.getClass();
        if (clazz.isInterface()) {
            return getJdkProxy(obj);
        } else {
            return getCglibProxy(obj);
        }
    }

    /**
     * Jdk动态代理
     *
     * @param obj 委托对象
     * @return 代理对象
     */
    public Object getJdkProxy(Object obj) {

        // 获取代理对象
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Object result = null;

                        boolean isTransaction = isProxyTransactional(obj, method);
                        try {
                            if (isTransaction) {
                                // 开启事务(关闭事务的自动提交)
                                transactionManager.beginTransaction();
                                result = method.invoke(obj, args);
                                // 提交事务
                                transactionManager.commit();
                            } else {
                                result = method.invoke(obj, args);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            // 回滚事务
                            transactionManager.rollback();

                            // 抛出异常便于上层servlet捕获
                            throw e;

                        }

                        return result;
                    }
                });

    }


    /**
     * 使用cglib动态代理生成代理对象
     *
     * @param obj 委托对象
     * @return
     */
    public Object getCglibProxy(Object obj) {
        return Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                Object result = null;

                boolean isTransaction = isProxyTransactional(obj, method);
                try {
                    if (isTransaction) {
                        // 开启事务(关闭事务的自动提交)
                        transactionManager.beginTransaction();
                        result = method.invoke(obj, objects);
                        // 提交事务
                        transactionManager.commit();
                    } else {
                        result = method.invoke(obj, objects);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    // 回滚事务
                    transactionManager.rollback();

                    // 抛出异常便于上层servlet捕获
                    throw e;

                }
                return result;
            }
        });
    }

    public boolean isProxyTransactional(Object object, Method method) {
        if (object.getClass().isAnnotationPresent(Transactional.class) ||
                method.isAnnotationPresent(Transactional.class)) {
            return true;
        } else {
            return false;
        }
    }
}
